package com.tiantianyuegou.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.GoodsModel;
import com.tiantianyuegou.util.ImageLoadeUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页商品列表
 * Created by huyang on 2016/1/6 0006.
 */
public class GoodsAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsModel> data;


    public GoodsAdapter(Context context) {
        this.context = context;
        data=new ArrayList<>();
    }

    public GoodsAdapter(Context context, List<GoodsModel> data) {
        this.context = context;
        this.data = data;
    }
    public void setData(List<GoodsModel> data){
        this.data.clear();
        this.data.addAll(data);
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        GoodsModel model=data.get(position);
        final ViewHolder holder;
        if (converView==null){
            converView= LayoutInflater.from(context).inflate(R.layout.grid_item_tuijian, null);
            holder=new ViewHolder();
            holder.name= (TextView) converView.findViewById(R.id.tv_goodsname);
            holder.price= (TextView) converView.findViewById(R.id.tv_goodsprice);
            holder.photo= (ImageView) converView.findViewById(R.id.iv_goods);
            converView.setTag(holder);
        }else{
            holder= (ViewHolder) converView.getTag();
        }
        holder.name.setText(model.getGoodsName()+"\n");
        Double price= Double.valueOf(model.getGoodsPrice());
        holder.price.setText("¥"+new DecimalFormat("0.00").format(price));
//        holder.redBag.setText(model.getRedBagCount());
//        ImageLoader.getInstance().init(
//                ImageLoaderConfiguration.createDefault(context));
//        ImageLoadeUtil imageLoadeUtil = new ImageLoadeUtil();
//
//        imageLoadeUtil.getPicture(model.getGoodsPhoto(),holder.photo);
        ImageLoader.getInstance().displayImage(ApiDefine.SERVER_URL+model.getGoodsPhoto().trim(),holder.photo);
//        holder.photo.setImageResource(R.drawable.sh);
        Log.e("===============","传递的数据是"+ApiDefine.SERVER_URL+model.getGoodsPhoto()+"---------------------");
        return converView;
    }
    class ViewHolder{
        TextView name;
        TextView price;
        ImageView photo;
    }

}